//
//  ViewController.swift
//  stepNum
//
//  Created by Pengwei on 2019/5/6.
//  Copyright © 2019 Pengwei. All rights reserved.
//

import UIKit
import HealthKit
import PStepNum

class ViewController: UIViewController {
    
    @IBOutlet weak var stepNumLabel: UILabel!
    let healthStore = HKHealthStore()
    let pstep = PStep()
    var stepCount: String? = "0"
    let httpUrl = "http://192.168.2.28:8088/api/exchange"
    let headData = ["appId": "3252191199100",
                    "sigParam":  "66a4361bdf8941c780114723ca0f4409",
                    "auth": "MzI1MjE5MTE5OTEwMC0yMDE5MDUxOA=="]
    let userId = "1234567890"
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    /// 步数上传服务器
    ///
    /// - Parameter sender: 
    @IBAction func stepToServer(_ sender: Any) {
        pstep.setpCount2Server(Url: httpUrl, Headers: headData, Uid: userId){ (res, error) in
            print("请求返回数据==", res as Any , error as Any)
            if error == nil {
                print("上传成功=")
            }else{
                 self.stepNumLabel.text = "上传失败"
                print("上传失败=", error as Any)
            }
        }
    }
    
    
    /// 获取步数
    ///
    /// - Parameter sender:
    @IBAction func getStepForHealth(_ sender: Any) {
        pstep.getStepCount(){(count:String?, error: Error?) in
            print("错误=", error as Any)
            if error == nil {
                DispatchQueue.main.async {
                    self.stepNumLabel.text = count
                }
            }else{
                print("执行错误")
            }
        }
    }
    
}


